<template>
  <div class="content">
    <div class="table">
      <el-table :data="tableData" border style="width: 100%" v-loading="loading">
        <el-table-column prop="id" label="订单号" align="center"></el-table-column>
        <el-table-column prop="CreatTime" label="日期" align="center"></el-table-column>
        <el-table-column prop="LineType" label="快递类型" align="center"></el-table-column>
        <el-table-column prop="mobile" label="联系电话" align="center"></el-table-column>
        <el-table-column label="详细地址" align="center">
          <template slot-scope="scope">
            {{ scope.row.area + scope.row.DetailedAddress }}
          </template>
        </el-table-column>
        <el-table-column prop="status" width="300" label="状态" align="center">
          <template slot-scope="scope">
            <el-row type="flex">
              <el-col :span="12">
                <el-select v-model="scope.row.status" placeholder="请选择" @change="statusChange($event, scope.row.id)">
                  <el-option v-for="item in status_options" :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-col>
              <el-col style="margin: auto;" :offset="2" :span="8">{{ '（' + scope.row.dabaoType + '）' }}</el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column fixed="right" v-if="$_permissions() == '2' || $_permissions() == '3'" label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button @click="handleTable(scope.row, true)" type="text" size="small">预览</el-button>
            <el-button @click="handleTable(scope.row, false)" type="text" size="small">编辑</el-button>
            <el-button @click="handleClick(scope.row, false)" type="text" size="small" v-if="$_permissions() == '3'">删除</el-button>
            <!-- <el-button @click="handleClick(scope.row, true)" type="text" size="small">编辑</el-button> -->
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagination.start"
      :page-sizes="[10, 25, 50, 100]"
      :page-size="pagination.nums"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total"
    ></el-pagination>
    <el-dialog append-to-body :title="data.id ? '编辑' : '添加'" :visible.sync="showTable" @close="close" width="60%">
      <order-table v-if="showTable" ref="order" :readonly="readonly" :data="data"></order-table>
      <el-button slot="footer" @click="save">保存</el-button>
    </el-dialog>
  </div>
</template>

<script>
import { showdaidabaoDetails, changeBG, delZYorder, updateZYorder, doZYorder, updateZyStatus } from '@/api/test.js';
import pagination from '@/mixins/page.js';
import orderTable from '@/components/table/index.vue';
export default {
  name: 'hongcaitj',
  components: {
    orderTable
  },
  mixins: [pagination],
  props: {
    type: {
      default: '普通'
    }
  },
  data() {
    return {
      tableData: [],
      showTable: false,
      loading: false, //加载中
      status_options: [
        {
          label: '已付款暂不发货',
          value: '已付款暂不发货'
        },
        {
          label: '等待打包',
          value: '等待打包'
        },
        {
          label: '打包中',
          value: '打包中'
        },
        {
          label: '已打包待付款',
          value: '已打包待付款'
        },
        {
          label: '已发货',
          value: '已发货'
        },
        {
          label: '已签收',
          value: '已签收'
        }
      ],
      data: {},
      readonly: false //是否只读
    };
  },
  methods: {
    statusChange(status, id) {
      //状态改变时触发
      console.log(status, id);
      updateZyStatus({ status, id }).then(res => {
        if (res.msg.indexOf('成功') != -1) {
          this.$message.success(res.msg);
          this.getList();
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    close() {
      this.data = {};
    },
    save() {
      var data = this.$refs['order'].data;
      console.log(data);
      var obj = {
        ...data,
        ...{
          consignee: data.consignee,
          mobile: data.mobile,
          area: data.area,
          DetailedAddress: data.DetailedAddress,
          LineType: data.LineType,
          AllWeights: data.list_info.reduce((prev, next) => {
            return prev + parseFloat(next.weights || 0);
          }, 0),
          DeclareMoney: data.sb_price, //申报金额
          DeclareNumber: data.sb_num, //申报数量
          IsReinforce: data.IsReinforce,
          KeepReceipts: data.KeepReceipts,
          InsuranceType: data.InsuranceType,
          InsuranceMoney: data.InsuranceType == '免费保险' ? '' : data.InsuranceMoney,
          descr: data.descr,
          // status:'未发货',
          OrderAmount: '', //总价格
          CostDetail: '',
          PointsCartonNumber: '',
          InternationalNumber: '',
          BoxFee: '',
          ReinforceFee: '',
          PackagPicSrc: '',
          DeclareInfo: data.list_datalist_sb.map(item => `${item.name}|${item.num}|${item.price}`).join('#'),
          ShippingList: data.list_info
            .map(item => {
              return item.OverseasCourierNumber + '|' + item.notes + '|' + item.weights;
            })
            .join('##')
        }
      };
      console.log(obj);
      if (obj.id) {
        updateZYorder(obj).then(res => {
          if (res.msg.indexOf('成功') != -1) {
            this.$message.success(res.msg);
            this.getList();
          } else {
            this.$message.error(res.msg);
          }
        });
      } else {
        doZYorder(obj).then(res => {
          if (res.msg.indexOf('成功') != -1) {
            this.$message.success(res.msg);
            this.showTable = false;
            this.getList();
          } else {
            this.$message.error(res.msg);
          }
        });
      }
    },
    handleTable(row, status) {
      this.data = row;
      this.readonly = status;
      this.showTable = true;
    },
    handleClick(row, status) {
      //status true:编辑 false:删除
      if (status) {
        //编辑
      } else {
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            delZYorder({ id: row.id, vipid: row.vipid }).then(res => {
              if (res.msg.indexOf('成功') != -1) {
                this.$message.success(res.msg);
                this.getList();
              } else {
                this.$message.error(res.msg);
              }
            });
          })
          .catch(() => {});
      }
    },
    getList() {
      //获取列表
      this.loading = true; //开始加载
      showdaidabaoDetails({ ...this.pagination, ...{ type: this.type } }).then(res => {
        console.log(res);
        this.tableData = res.data;
        this.pagination.total = res.total;
        this.loading = false; //结束加载
      });
    },
    changeOption() {
      //状态改变时同步列表
      this.getList();
    }
  },
  mounted() {
    this.getList();
  }
};
</script>

<style lang="scss" scoped>
.content {
  background-color: #fff !important;
  height: 100% !important;
  padding: 0 !important;
}
</style>
